﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace _0712502_0712504_DesignPatternGame
{
    public class MapDrawer
    {
        public Texture2D[] Meterials;
        public int[,] _mapMatrix;
        public int nColumn, nRow;
        public Point MeterialRealSize;
        public Point MeterialDrawingSize;

        public int value;

        protected Point _startDrawingPoint;
        protected Point StartDrawingPoint
        {
            get { return _startDrawingPoint; }
            set { _startDrawingPoint = value; }
        }

        public MapDrawer()
        {

        }
        public MapDrawer(Texture2D[] meterials)
        {            
            this.Meterials = meterials;
        }

        public void LoadMapMatrix(MapMatrix mapMatrix)
        {
            _mapMatrix = mapMatrix.Matrix;
            StartDrawingPoint = mapMatrix.StartDrawingPoint;
            this.MeterialDrawingSize = mapMatrix.ImgDrawingSize;
            this.MeterialRealSize = mapMatrix.ImgRealSize;
            this.nColumn = mapMatrix.numofColumn;
            this.nRow = mapMatrix.numofRow;
        }

        public void DrawLeftToRightToBottom(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int row = 0; row < this.nRow; row++)
            {
                for (int col = 0; col < this.nColumn; col++)
                {
                    value = _mapMatrix[row, col];

                    if (value != 0)
                    {
                        if (value > -1 && value < 16)
                        {
                            int X, Y;

                            X = StartDrawingPoint.X + col * MeterialDrawingSize.X;//(int)_imgSize.X;
                            Y = StartDrawingPoint.Y + row * MeterialDrawingSize.Y;//(int)_imgSize.Y;

                            spriteBatch.Draw(Meterials[value - 1], new Vector2(X, Y),
                                new Rectangle(0, 0, MeterialRealSize.X, MeterialRealSize.Y),
                                Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
                        }
                    }
                }
            }
        }

        public void DrawRightToLeftToBottom(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int row = 0; row < this.nRow; row++)
            {
                for (int col = this.nColumn - 1; col > -1; col--)
                {
                    value = _mapMatrix[row, col];

                    if (value != 0)
                    {
                        if (value > -1 && value < 16)
                        {
                            int X, Y;

                            X = StartDrawingPoint.X + col * MeterialDrawingSize.X;//(int)_imgSize.X;
                            Y = StartDrawingPoint.Y + row * MeterialDrawingSize.Y;//(int)_imgSize.Y;

                            spriteBatch.Draw(Meterials[value - 1], new Vector2(X, Y),
                                new Rectangle(0, 0, MeterialRealSize.X, MeterialRealSize.Y),
                                Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
                        }
                    }
                }
            }
        }
    }
}
